package com.linx.rxdata

import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject

class RxData<T>(value: T) {

    @Transient
    private var subject: Subject<T>? = null

    private var data: T = value
        set(value) {
            if (field != value) {
                field = value
                subject?.onNext(value)
            }
        }

    fun set(value: T) {
        data = value
    }

    fun get(): T {
        return data
    }

    fun observe(): Observable<T> {
        if (subject == null) {
            subject = BehaviorSubject.create()
            subject?.onNext(data)
        }
        return subject!!
    }

}